package com.example.demo.service;

import com.example.demo.entity.pojo.*;
import com.example.demo.entity.vo.BookVo;
import com.example.demo.entity.vo.RecordVo;
import com.example.demo.entity.vo.StudentVo;
import com.example.demo.exception.LoginException;
import com.example.demo.mapper.BookMapper;
import com.example.demo.mapper.RecordMapper;
import com.example.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RecordService {
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    CostRecordService costRecordService;

    /**
     * 借书纪录添加纪录
     *
     * @param cardId 学生id
     * @param bookId 书籍id
     * @return
     */
    @Transactional
    public boolean borrowBook(int cardId, int bookId) throws LoginException {
        //TODO 判断是否能借
        //拿到学生借书卡信息
        Card cardMessage = studentMapper.getCardMessage(cardId);
        if (cardMessage == null){
            return false;
        }
        //拿到学生借书卡的等级id
        int level = cardMessage.getLevelId();
        //获取改等级能借书的集合
        List<LevelType> type = studentMapper.getType(cardId);
        List<Integer> list = new ArrayList<>();
        for (LevelType levelType : type) {
            list.add(levelType.getTypeId());
        }
        //获取所借书籍的信息
        Book bookMessage = bookMapper.getBookMessage(bookId);
        if (bookMessage == null){
            return false;
        }
        //获取所借书籍的剩余数量
        int state = bookMessage.getCount();
        if (state == 0) {
            return false;
        }
        //获取所借书籍的类型
        int typeId = bookMessage.getTypeId();
        //判断所借的书籍是否可以借
        if (!list.contains(typeId)) {
            return false;
        }
        //判断还能借基本
        if ((cardMessage.getCount() < 1 )) {
            return false;
        }

        //查询该类型借了几本
        List<BookRecord> borrowBookByTypeNumber = recordMapper.getBorrowBookByTypeNumber(cardId, bookMessage.getTypeId());
        int borrowNum = borrowBookByTypeNumber.size();
        //判断借该类型的书要花多少钱
        for (LevelType levelType : type) {
            if (levelType.getLevelId() == level) {
                if (levelType.getTypeId() == typeId) {
                    //如何改类型还可以借
                    if (levelType.getBorrowNumber() > borrowNum){
                        if (cardMessage.getMoney() > levelType.getCost()) {
                            //可以借
                            // 去流水加纪录
                            if (costRecordService.addCostRecord(levelType.getCost() * -1, cardId,1)) {
                                //TODO 修改卡的余额
                                double newMoney = cardMessage.getMoney()+(levelType.getCost()*-1);
                                int consumption = studentMapper.consumption(newMoney, cardId);
                                if (consumption > 0){
                                    //加成功 去借书纪录加纪录
                                    int i = recordMapper.borrowBook(cardId, bookId,bookMessage.getTypeId());
                                    if (i > 0) {
                                        return true;
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }
        return false;

    }


    /**
     * 根据书籍id获取纪录
     * @param id
     * @return
     */
    public BookRecord getBookById(int id) {
//        System.out.println(recordMapper.getBookById(id));
        return recordMapper.getBookById(id);
    }

    /**
     * 归还书籍
     *
     * @param id
     * @return
     */
    public boolean returnBook(int id) {
        int i = recordMapper.returnBook(id);
        return i > 0;
    }


    /**
     * 获取借书记录
     * @param id 卡id
     * @return
     */
    public List<RecordVo> getRecord(int id){
        List<BookRecord> record = recordMapper.getRecord(id);
        List<RecordVo> recordVos = new ArrayList<>();
        Card cardMessage = studentMapper.getCardMessage(id);
        int i = 1;
        for (BookRecord b : record){

            if (b.getTime() == null && System.currentTimeMillis() > b.getReturnTime().getTime()){
                //逾期未归还
                recordMapper.changeState(b.getId(),3);
            }
            if (b.getTime() != null && b.getTime().getTime() > b.getReturnTime().getTime()){
                recordMapper.changeState(b.getId(),2);
            }
            Book bookMessage = bookMapper.getBookMessage(b.getBookId());
            RecordVo recordVo = new RecordVo(b,cardMessage,bookMessage);
            recordVo.setId(i);
            i++;
            recordVos.add(recordVo);
        }

//        RecordVo recordVo = new RecordVo(record);
        return recordVos;
    }


    /**
     * 根据书籍名称和作者名称查询书籍纪录
     * @param name
     * @param  id 卡的id
     * @return
     */
    public List<RecordVo> getRecordByBookName(String name,Integer id,String author){
        Card cardMessage = studentMapper.getCardMessage(id);
        List<Book> bookByName = bookMapper.getBookByName(name,author);
        Set<Integer> set = new HashSet<>();
        for (Book book : bookByName) {
            set.add(book.getId());
        }
        List<RecordVo> recordVos = new ArrayList<>();
        int i = 1;
        for (Integer integer : set) {
           List<BookRecord> recordByBookName = recordMapper.getRecordByBookName(integer,id);
            Book bookMessage = bookMapper.getBookMessage(integer);
            for (BookRecord bookRecord : recordByBookName) {
                RecordVo recordVo = new RecordVo(bookRecord,cardMessage,bookMessage);
                recordVo.setId(i);
                i++;
                recordVos.add(recordVo);
            }
        }
        return recordVos;
    }

}
